package com.molyfun.parents.activity.msg;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.rest.bean.NoticeDetailResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/21/16.
 */
public class PushNoticeActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.base_title_left)
    ImageView mTitleLeft;
    @InjectView(R.id.push_notice_title)
    TextView mNoticeTitle;
    @InjectView(R.id.push_notice_author)
    TextView mNoticeAuthor;
    @InjectView(R.id.push_notice_time)
    TextView mNoticeTime;
    @InjectView(R.id.push_notice_content)
    TextView mPushContent;
    @InjectView(R.id.push_notice_attachment_layout)
    AttachmentViewer attachmentViewer;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;

    private String mNoticeDetailId;
    private String mMsgId;


    public static final void startActivity(Context context, String detailId) {
        Intent intent = new Intent();
        intent.setClass(context, PushNoticeActivity.class);
        intent.putExtra(Const.DETAIL_ID, detailId);
        context.startActivity(intent);
    }

    public static final void startActivity(Context context, String detailId, String msgId) {
        Intent intent = new Intent();
        intent.setClass(context, PushNoticeActivity.class);
        intent.putExtra(Const.DETAIL_ID, detailId);
        intent.putExtra(Const.MSG_ID, msgId);
        context.startActivity(intent);
    }

    public static final void startActivityNewTask(Context context, String detailId) {
        Intent intent = new Intent();
        intent.setClass(context, PushNoticeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Const.DETAIL_ID, detailId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_notice);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");

        mBaseTitle.setText("消息详情");

        mTitleLeft.setOnClickListener(this);

        mNoticeDetailId = getIntent().getStringExtra(Const.DETAIL_ID);
        mMsgId = getIntent().getStringExtra(Const.MSG_ID);
//        if(!TextUtils.isEmpty(mMsgId)){
//            setMsgViewed(mMsgId);
//        }
        if(!TextUtils.isEmpty(mNoticeDetailId)){
            getNoticeDetail(mNoticeDetailId);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    private void getNoticeDetail(final String mNoticeDetailId){
        showProgressDialog();
        Call<NoticeDetailResponse> call = MFPApplication.getInstance().getRestClient().getNoticeDetail(mNoticeDetailId);
        call.enqueue(new Callback<NoticeDetailResponse>() {
            @Override
            public void onResponse(Call<NoticeDetailResponse> call, Response<NoticeDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    NoticeDetailResponse.NoticeDetailBean noticeDetailBean = response.body().getData();
                    mNoticeTitle.setText(noticeDetailBean.getTitle());
                    mPushContent.setText(noticeDetailBean.getContent());
                    attachmentViewer.initData(noticeDetailBean.getMediaobjectlist());
                    mNoticeAuthor.setText("---" + noticeDetailBean.getDepartname());
                    mNoticeTime.setText(TimeUtils.transForDateTime(noticeDetailBean.getCreatedatetime()));
                }
            }

            @Override
            public void onFailure(Call<NoticeDetailResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
